﻿using markdown_picture_packer.common.constants;
using markdown_picture_packer.common.enums;
using markdown_picture_packer.common.utils;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace markdown_picture_packer.frontend.panels
{
    public class ExportPathPanel : BasePanel
    {
        private ComboBoxPanel Cbp;
        private int CbpWidth = 223;
        private int CbpAllWidth;
        private SelectPathPanel Spp;

        public override void InitComponent()
        {
            Cbp = new ComboBoxPanel("导出路径", new string[] {
                EnumDictionary.EXPORT_PATH_TYPE[ExportPathType.CUSTOM],
                EnumDictionary.EXPORT_PATH_TYPE[ExportPathType.DESKTOP]
            });
            Spp = new SelectPathPanel(SelectPathType.DIRECTORY);
        }
        public override void InitSize(int newWdith, int newHeight)
        {
            Cbp.Size = new Size(CbpWidth, newHeight);
            CbpAllWidth = newWdith;
            Spp.Size = new Size(FrontUtil.GetFillWidth(newWdith, Cbp), newHeight);
        }
        public override void InitLocation()
        {
            Cbp.Location = new Point(0, 0);
            FrontUtil.OnRight(Spp, Cbp, 0);
        }
        public override void InitEvent()
        {
            base.InitEvent();
            Cbp.GetComboBox().SelectedIndexChanged += (s, e) =>
            {
                Spp.Visible = Cbp.GetText().Equals(EnumDictionary.EXPORT_PATH_TYPE[ExportPathType.CUSTOM]);
                Cbp.Size = new Size(Spp.Visible ? CbpWidth : CbpAllWidth, Cbp.Height);
            };
        }
        public override void AddComponent()
        {
            Controls.Add(Cbp);
            Controls.Add(Spp);
        }
        public void SelectComboBox(int index)
        {
            Cbp.GetComboBox().SelectedIndex = index;
        }
        public string GetPath()
        {
            if (Cbp.GetText()?.Equals(EnumDictionary.EXPORT_PATH_TYPE[ExportPathType.DESKTOP]) == true) return Constants.DESKTOP_PATH;
            string customPath = Spp.GetVal();
            return !string.IsNullOrEmpty(customPath) && Directory.Exists(customPath) ? customPath : null;
        }
    }
}
